package com.appspot.yozmcup.oauth;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;

@SuppressWarnings("serial")
public class OAuthRequest extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
		HttpSession session = req.getSession();
		
		OAuthConsumer consumer = OAuthInfo.getConsumer();
		OAuthProvider provider = OAuthInfo.getProvider();
		
		String oauthURL = "";
		try {
			oauthURL = provider.retrieveRequestToken(consumer, OAuthInfo.URL_CALLBACK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		session.setAttribute("consumer", consumer);
		session.setAttribute("provider", provider);

		res.sendRedirect(oauthURL);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
		HttpSession session = req.getSession();
		
		OAuthConsumer consumer = OAuthInfo.getConsumer();
		OAuthProvider provider = OAuthInfo.getProvider();
		String oauthURL = "";
		try {
			oauthURL = provider.retrieveRequestToken(consumer, OAuthInfo.URL_CALLBACK);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		session.setAttribute("consumer", consumer);
		session.setAttribute("provider", provider);

		res.sendRedirect(oauthURL);
	}
}
